// default package
// Generated Jul 28, 2012 3:15:14 PM by Hibernate Tools 3.4.0.CR1
package cl.usm.taller.corona.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Example;

import cl.usm.taller.corona.model.Usuario;
import cl.usm.taller.corona.util.HibernateUtil;

/**
 * Home object for domain model class Usuario.
 * @see .Usuario
 * @author Hibernate Tools
 */
public class UsuarioDao {

	private static final Log log = LogFactory.getLog(UsuarioDao.class);
	
	public void persist(Usuario transientInstance) {
		log.debug("persisting Usuario instance");
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        session.save(transientInstance);
	        session.getTransaction().commit();	        
	        log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Usuario instance) {
		log.debug("attaching dirty Usuario instance");
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.saveOrUpdate(instance);
			session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Usuario instance) {
		log.debug("attaching clean Usuario instance");
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.lock(instance, LockMode.NONE);
			session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Integer id) {
		log.debug("Delete Usuario instance");
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        Usuario usuario = (Usuario) session.load(Usuario.class, id);
	        if(null != usuario) {
	            session.delete(usuario);
	        }
	        session.getTransaction().commit();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Usuario merge(Usuario detachedInstance) {
		log.debug("merging Usuario instance");
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Usuario result = (Usuario) session.merge(detachedInstance);
			session.getTransaction().commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Usuario findById(java.lang.Integer id) {
		log.debug("getting Usuario instance with id: " + id);
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Usuario instance = (Usuario) session.get("Usuario", id);
			session.getTransaction().commit();
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public boolean login(Usuario instance) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
        
        try {
             
        	Query query = session.createQuery("from Usuario where userName=:username and password = :password ");
        	query.setParameter("username", instance.getUserName());
        	query.setParameter("password", instance.getPassword());
        	
        	@SuppressWarnings("unchecked")
			List<Usuario> list = (List<Usuario>) query.list();
        	session.getTransaction().commit();
        	
        	if(list.isEmpty()){
        		return false;
        	}else{
        		return true;
        	}

             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }
    }
	
	public List<Usuario> list() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
        List<Usuario> usuarios = null;
        try {
             
        	usuarios = (List<Usuario>)session.createQuery("from Usuario").list();
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return usuarios;
    }
	
	public List<Usuario> findByExample(Usuario instance) {
		log.debug("finding Usuario instance by example");
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			List<Usuario> results = (List<Usuario>)session.createCriteria("Usuario").add(Example.create(instance)).list();
			session.getTransaction().commit();
			
			log.debug("find by example successful, result size: "
					+ results.size());
			
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
